{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "32f0866e-e296-4aab-bdb2-21cfe82648c2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:11:50.454740Z",
     "iopub.status.busy": "2022-05-21T12:11:50.454240Z",
     "iopub.status.idle": "2022-05-21T12:11:50.724937Z",
     "shell.execute_reply": "2022-05-21T12:11:50.724160Z",
     "shell.execute_reply.started": "2022-05-21T12:11:50.454666Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b5212bb1-d6c0-47d9-88e6-d7c36f04097c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:11:53.212879Z",
     "iopub.status.busy": "2022-05-21T12:11:53.212543Z",
     "iopub.status.idle": "2022-05-21T12:11:53.268560Z",
     "shell.execute_reply": "2022-05-21T12:11:53.267391Z",
     "shell.execute_reply.started": "2022-05-21T12:11:53.212854Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RawData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.102020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.113755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.080326</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.945521</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.194798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.613031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-1.958032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-1.484649</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    RawData\n",
       "0  0.102020\n",
       "1 -0.113755\n",
       "2  1.080326\n",
       "3 -1.945521\n",
       "4 -0.194798\n",
       "5 -0.613031\n",
       "6 -1.958032\n",
       "7 -1.484649"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "data = pd.DataFrame({\"RawData\": np.random.randn(8)})\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b92433b-de90-44cf-bde7-cb8713d896b9",
   "metadata": {},
   "source": [
    "## 一、离散化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a070ce69-6cd7-4bc9-b17f-872bfb2a2e92",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:13:13.761573Z",
     "iopub.status.busy": "2022-05-21T12:13:13.761246Z",
     "iopub.status.idle": "2022-05-21T12:13:13.772719Z",
     "shell.execute_reply": "2022-05-21T12:13:13.771546Z",
     "shell.execute_reply.started": "2022-05-21T12:13:13.761548Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RawData</th>\n",
       "      <th>Discret</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.102020</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.113755</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.080326</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.945521</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.194798</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.613031</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-1.958032</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-1.484649</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    RawData  Discret\n",
       "0  0.102020        7\n",
       "1 -0.113755        4\n",
       "2  1.080326        8\n",
       "3 -1.945521        6\n",
       "4 -0.194798        5\n",
       "5 -0.613031        2\n",
       "6 -1.958032        1\n",
       "7 -1.484649        3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 离散化处理成大小序列\n",
    "data[\"Discret\"] = data[\"RawData\"].argsort() + 1\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf648b80-bef8-48bc-ae3f-992c069ceeb1",
   "metadata": {},
   "source": [
    "## 二、面元划分"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4aa222a1-194c-42a4-8a32-f540e08c20df",
   "metadata": {},
   "source": [
    "### 2.1 cut函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4bfe9138-b6b6-4eb4-9478-a55a03c5bc68",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:16:45.954041Z",
     "iopub.status.busy": "2022-05-21T12:16:45.953709Z",
     "iopub.status.idle": "2022-05-21T12:16:45.962994Z",
     "shell.execute_reply": "2022-05-21T12:16:45.961998Z",
     "shell.execute_reply.started": "2022-05-21T12:16:45.954017Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RawData</th>\n",
       "      <th>Discret</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.102020</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.113755</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.080326</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.945521</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.194798</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.613031</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-1.958032</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-1.484649</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    RawData  Discret\n",
       "0  0.102020        7\n",
       "1 -0.113755        4\n",
       "2  1.080326        8\n",
       "3 -1.945521        6\n",
       "4 -0.194798        5\n",
       "5 -0.613031        2\n",
       "6 -1.958032        1\n",
       "7 -1.484649        3"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看data\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3f3a82f7-a8fa-4320-b7a1-09b5499e8afc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:18:39.185152Z",
     "iopub.status.busy": "2022-05-21T12:18:39.184706Z",
     "iopub.status.idle": "2022-05-21T12:18:39.195326Z",
     "shell.execute_reply": "2022-05-21T12:18:39.194026Z",
     "shell.execute_reply.started": "2022-05-21T12:18:39.185126Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     (-1.5, 0.5]\n",
       "1     (-1.5, 0.5]\n",
       "2      (0.5, 1.5]\n",
       "3    (-2.0, -1.5]\n",
       "4     (-1.5, 0.5]\n",
       "5     (-1.5, 0.5]\n",
       "6    (-2.0, -1.5]\n",
       "7     (-1.5, 0.5]\n",
       "Name: RawData, dtype: category\n",
       "Categories (3, interval[float64, right]): [(-2.0, -1.5] < (-1.5, 0.5] < (0.5, 1.5]]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 设定面元划分标准\n",
    "bins = [-2, -1.5, 0.5, 1.5]\n",
    "# 进行面元划分\n",
    "cats = pd.cut(data[\"RawData\"], bins)\n",
    "# pd.cut(data[\"RawData\"], [-2, -1.5, 0.5, 1.5])\n",
    "cats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7d8955d8-8301-4ac8-99bd-5d7b6b072880",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:20:49.715416Z",
     "iopub.status.busy": "2022-05-21T12:20:49.715084Z",
     "iopub.status.idle": "2022-05-21T12:20:49.724928Z",
     "shell.execute_reply": "2022-05-21T12:20:49.723500Z",
     "shell.execute_reply.started": "2022-05-21T12:20:49.715391Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-1.5, 0.5]     5\n",
       "(-2.0, -1.5]    2\n",
       "(0.5, 1.5]      1\n",
       "Name: RawData, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计每个面元区间里分别有多少个元素\n",
    "pd.value_counts(cats)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fff49bc1-fd33-40a6-b739-9e28ecb304a9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:21:31.750859Z",
     "iopub.status.busy": "2022-05-21T12:21:31.750450Z",
     "iopub.status.idle": "2022-05-21T12:21:31.760425Z",
     "shell.execute_reply": "2022-05-21T12:21:31.759452Z",
     "shell.execute_reply.started": "2022-05-21T12:21:31.750831Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     [-1.5, 0.5)\n",
       "1     [-1.5, 0.5)\n",
       "2      [0.5, 1.5)\n",
       "3    [-2.0, -1.5)\n",
       "4     [-1.5, 0.5)\n",
       "5     [-1.5, 0.5)\n",
       "6    [-2.0, -1.5)\n",
       "7     [-1.5, 0.5)\n",
       "Name: RawData, dtype: category\n",
       "Categories (3, interval[float64, left]): [[-2.0, -1.5) < [-1.5, 0.5) < [0.5, 1.5)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 进行面元划分，设置左闭右开\n",
    "pd.cut(data[\"RawData\"], bins, right=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a46a5397-cb3c-46d0-9d98-00da9af5ffc5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:22:59.203974Z",
     "iopub.status.busy": "2022-05-21T12:22:59.203630Z",
     "iopub.status.idle": "2022-05-21T12:22:59.212937Z",
     "shell.execute_reply": "2022-05-21T12:22:59.211578Z",
     "shell.execute_reply.started": "2022-05-21T12:22:59.203948Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    中区间\n",
       "1    中区间\n",
       "2    高区间\n",
       "3    低区间\n",
       "4    中区间\n",
       "5    中区间\n",
       "6    低区间\n",
       "7    中区间\n",
       "Name: RawData, dtype: category\n",
       "Categories (3, object): ['低区间' < '中区间' < '高区间']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 进行面元划分,设置面元标签\n",
    "# 设定面元标签\n",
    "labels = [\"低区间\", \"中区间\", \"高区间\"]\n",
    "# 设定面元划分标准\n",
    "bins = [-2, -1.5, 0.5, 1.5]\n",
    "# 进行面元划分\n",
    "pd.cut(data[\"RawData\"], bins, labels=labels)\n",
    "# pd.cut(data[\"RawData\"], bins, labels=[\"低区间\", \"中区间\", \"高区间\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fc7ee647-0387-4340-a578-e890e220c84a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:24:29.293151Z",
     "iopub.status.busy": "2022-05-21T12:24:29.292817Z",
     "iopub.status.idle": "2022-05-21T12:24:29.302627Z",
     "shell.execute_reply": "2022-05-21T12:24:29.301502Z",
     "shell.execute_reply.started": "2022-05-21T12:24:29.293125Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    (-0.44, 0.32]\n",
       "1    (-0.44, 0.32]\n",
       "2     (0.32, 1.08]\n",
       "3    (-1.96, -1.2]\n",
       "4    (-0.44, 0.32]\n",
       "5    (-1.2, -0.44]\n",
       "6    (-1.96, -1.2]\n",
       "7    (-1.96, -1.2]\n",
       "Name: RawData, dtype: category\n",
       "Categories (4, interval[float64, right]): [(-1.96, -1.2] < (-1.2, -0.44] < (-0.44, 0.32] < (0.32, 1.08]]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 进行面元划分,分成4个面元\n",
    "# 数据区间均分为4个面元\n",
    "# 2位有效数字\n",
    "pd.cut(data[\"RawData\"], 4, precision=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ecbccb20-64a5-4d95-9d6c-b30ae7d89d50",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:31:57.095658Z",
     "iopub.status.busy": "2022-05-21T12:31:57.095319Z",
     "iopub.status.idle": "2022-05-21T12:31:57.105823Z",
     "shell.execute_reply": "2022-05-21T12:31:57.104625Z",
     "shell.execute_reply.started": "2022-05-21T12:31:57.095631Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    (-0.06, 1.08]\n",
       "1    (-0.4, -0.06]\n",
       "2    (-0.06, 1.08]\n",
       "3    (-1.97, -1.6]\n",
       "4    (-0.4, -0.06]\n",
       "5     (-1.6, -0.4]\n",
       "6    (-1.97, -1.6]\n",
       "7     (-1.6, -0.4]\n",
       "Name: RawData, dtype: category\n",
       "Categories (4, interval[float64, right]): [(-1.97, -1.6] < (-1.6, -0.4] < (-0.4, -0.06] < (-0.06, 1.08]]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 进行面元划分,分成4个面元\n",
    "# 数据均匀分布在4个面元\n",
    "# 2位有效数字\n",
    "pd.qcut(data[\"RawData\"], 4, precision=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "74dca2e4-c1d4-49f6-a702-1117a6345e82",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:34:53.368252Z",
     "iopub.status.busy": "2022-05-21T12:34:53.367924Z",
     "iopub.status.idle": "2022-05-21T12:34:53.379967Z",
     "shell.execute_reply": "2022-05-21T12:34:53.379280Z",
     "shell.execute_reply.started": "2022-05-21T12:34:53.368228Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-1.96, -1.2]    3\n",
       "(-0.44, 0.32]    3\n",
       "(-1.2, -0.44]    1\n",
       "(0.32, 1.08]     1\n",
       "Name: RawData, dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# cut均衡划分数据区间\n",
    "pd.value_counts(pd.cut(data[\"RawData\"], 4, precision=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddfb51b3-53b5-4cb8-a038-84fa868e7280",
   "metadata": {},
   "source": [
    "### 2.2 qcut函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2f24a806-36d4-44f9-a074-f4f38ff5c2bd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:34:57.072002Z",
     "iopub.status.busy": "2022-05-21T12:34:57.071678Z",
     "iopub.status.idle": "2022-05-21T12:34:57.081815Z",
     "shell.execute_reply": "2022-05-21T12:34:57.081148Z",
     "shell.execute_reply.started": "2022-05-21T12:34:57.071978Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-1.97, -1.6]    2\n",
       "(-1.6, -0.4]     2\n",
       "(-0.4, -0.06]    2\n",
       "(-0.06, 1.08]    2\n",
       "Name: RawData, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# qcut均匀划分数据分布\n",
    "pd.value_counts(pd.qcut(data[\"RawData\"], 4, precision=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5e65dc9a-8bf4-4fa1-8c54-39f88780fb52",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-21T12:59:56.044559Z",
     "iopub.status.busy": "2022-05-21T12:59:56.044232Z",
     "iopub.status.idle": "2022-05-21T12:59:56.057277Z",
     "shell.execute_reply": "2022-05-21T12:59:56.056016Z",
     "shell.execute_reply.started": "2022-05-21T12:59:56.044534Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-1.97, -0.4]    4\n",
       "(-0.4, -0.06]    2\n",
       "(-0.06, 1.08]    2\n",
       "Name: RawData, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# qcut指定面元分界\n",
    "pd.value_counts(pd.qcut(data[\"RawData\"], [0, 0.5, 0.75, 1], precision=2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
